package com.situ.company.score.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.score.model.ScoreModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class ScoreDao {
    
    public int insert(ScoreModel model) {
        String sql = "insert into Score (code_emp,code_pro,score) values(?,?,?)";
        return JDBCUtil.update(sql, model.getCodeEmp(),model.getCodePro(),model.getScore());
    }
    
    public int deleteById(ScoreModel model) {
        String sql = "delete from score where id=? ";
        return JDBCUtil.update(sql, model.getId());
    }
    
    public int deleteByCode(ScoreModel model) {
        String sql = "delete from score where code_emp=? and code_pro=? ";
        return JDBCUtil.update(sql, model.getCodeEmp(), model.getCodePro());
    }
    
    public int delete(ScoreModel model) {
        StringBuffer sql = new StringBuffer("delete from score ");
        List<Object> vals = appendWhere(sql, model);
        return JDBCUtil.update(sql.toString(), vals);
    }
    
    public int updateAllById(ScoreModel model) {
        String sql = "update score set code_emp=?, code_pro=?, score=? where id=? ";
        return JDBCUtil.update(sql, model.getCodeEmp(), model.getCodePro(), model.getScore(), model.getId());
    }
    
    public int updateAll(ScoreModel model) {
        String sql = "update score set score=? where code_emp=? and code_pro=?";
        return JDBCUtil.update(sql, model.getScore(), model.getCodeEmp(),model.getCodePro());
    }
    
    public int updateActiveById(ScoreModel model) {
        StringBuffer sql = new StringBuffer("update score set id=id ");
        List<Object> vals = new ArrayList<Object>();
        String codeEmp = model.getCodeEmp();
        if(codeEmp != null) {
            sql.append(" set code_emp=? ");
            vals.add(codeEmp);
        }
        String codePro = model.getCodePro();
        if(codePro != null) {
            sql.append(" set code_pro=? ");
            vals.add(codePro);
        }
        String score = model.getScore();
        if(score != null) {
            sql.append(" set score=? ");
            vals.add(score);
        }
        sql.append(" where id=? ");
        vals.add(model.getId());
        return JDBCUtil.update(sql.toString(), vals);
    }
    
    public int updateActiveByCode(ScoreModel model) {
        StringBuffer sql = new StringBuffer("update score set id=id ");
        List<Object> vals = new ArrayList<Object>();
        String score = model.getScore();
        if(score != null) {
            sql.append(" set score=? ");
            vals.add(score);
        }
        sql.append(" where code_emp=? and code_pro=? ");
        vals.add(model.getCodeEmp());
        vals.add(model.getCodePro());
        return JDBCUtil.update(sql.toString(), vals);
    }
    
    public List<ScoreModel> selectList(ScoreModel model){
        StringBuffer sql = new StringBuffer("select * from score ");
        List<Object> vals = appendWhere(sql, model);
        return JDBCUtil.queryList(sql.toString(), vals, ScoreModel.class, getFields());
    }
    
    public ScoreModel selectModel(ScoreModel model) {
        StringBuffer sql = new StringBuffer("select id, code_emp, code_pro, score from score ");
        List<Object> vals = appendWhere(sql, model);
        return JDBCUtil.queryModel(sql.toString(), vals, ScoreModel.class, getFields());
    }

    
    private List<Object> appendWhere(StringBuffer sql, ScoreModel model) {
        List<Object> vals = new ArrayList<>();
        sql.append(" where 1=1 ");
        Integer id = model.getId();
        if(id != null) {
            sql.append(" id = ? ");
            vals.add(id);
        }
        String codeEmp = model.getCodeEmp();
        if(!FmtEmpty.isEmpty(codeEmp)) {
            sql.append(" code_emp = ? ");
            vals.add(codeEmp);
        }
        String codePro = model.getCodePro();
        if(!FmtEmpty.isEmpty(codePro)) {
            sql.append(" code_pro = ? ");
            vals.add(codePro);
        }
        String score = model.getScore();
        if(!FmtEmpty.isEmpty(score)) {
            sql.append(" score = ? ");
            vals.add(score);
        }
        return vals;
    }
    
    private Map<String, String> getFields(){
        Map<String, String> fields = new HashMap<String, String>();
        fields.put("id", "id");
        fields.put("codeEmp", "code_emp");
        fields.put("codePro", "code_pro");
        fields.put("score", "score");
        return fields;
    }
}
